/***************************************************************************
File:        lineeditvalue.cpp
Description: Line edit linked to a variable

Copyright:   (C) 2000 by ZJ Laczik
Email:       Z.J.Laczik@oxford-optics.ac.uk
****************************************************************************
Change log:
  begin      Sun Apr 30 2000
   15/2/2012	QT4 update
***************************************************************************/

#include <stdlib.h>
#include <stdio.h>

#include <QKeyEvent>

#include "lineeditvalue.h"

LineEditValue::LineEditValue(QWidget *parent) : QLineEdit(parent) {
    setText("0");
    clearDirtyFlag();
    clearChangedFlag();

    connect(this, SIGNAL(textChanged(const QString &)), SLOT(setDirtyFlag()) );
}

LineEditValue::~LineEditValue() {
}

void LineEditValue::focusOutEvent ( QFocusEvent * ) {
    if (mDirty) {
        ValueChanged();
    };
}

void LineEditValue::keyPressEvent( QKeyEvent* e ) {
    if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
        if (mDirty) {
            ValueChanged();
        };
    }
    else {
        QLineEdit::keyPressEvent(e);
    };
    return;
}

void LineEditValue::clearDirtyFlag() {
    mDirty = false;
}

void LineEditValue::setDirtyFlag() {
    mDirty = true;
}

void LineEditValue::ValueChanged() {
    clearDirtyFlag();
    setChangedFlag();
    emit( ValueChanged( text().toUtf8().constData() ));
    emit( ValueChanged( text() ));
    emit( ValueChanged( (unsigned char)text().toInt() ));
    emit( ValueChanged( text().toInt() ));
    emit( ValueChanged( text().toLong() ));
    emit( ValueChanged( text().toFloat() ));
    emit( ValueChanged( text().toDouble() ));
    emit( ValueChanged( text().toShort() ));
}

void LineEditValue::setChangedFlag() {
    mChangedFlag=true;
}

void LineEditValue::clearChangedFlag() {
    mChangedFlag=false;
}

bool LineEditValue::getChangedFlag() {
    return mChangedFlag;
}

void LineEditValue::Update(const QString & newtxt)
{
    QString s(newtxt);

    if (s != text()) {
        setText(newtxt);
        ValueChanged();
    }
}

void LineEditValue::Update(const char * newtxt)
{
    QString s(newtxt);

    if (s != text()) {
        setText(newtxt);
        ValueChanged();
    }
}

void LineEditValue::Update(int newvalue)
{
    QString s;

    if (newvalue != text().toInt()) {
        s.sprintf( "%d", newvalue );
        setText( s );
        ValueChanged();
    }
}

void LineEditValue::Update(long int newvalue)
{
    QString s;

    if (newvalue != text().toLong()) {
        s.sprintf( "%ld", newvalue );
        setText( s );
        ValueChanged();
    }
}

void LineEditValue::Update(unsigned char newvalue)
{
    QString s;

    if (newvalue != (unsigned char)text().toInt()) {
        s.sprintf( "%d", (int)newvalue );
        setText( s );
        ValueChanged();
    }
}

void LineEditValue::Update(float newvalue)
{
    QString s;

    if (newvalue != text().toFloat()) {
        s.sprintf( "%.5g", (double)newvalue );
        setText( s );
        ValueChanged();
    }
}

void LineEditValue::Update(double newvalue)
{
    QString s;

    if (newvalue != text().toDouble()) {
        s.sprintf( "%.5g", newvalue );
        setText( s );
        ValueChanged();
    }
}
